﻿using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web.UI;
using Channelvn.Cached.Common;
using System.Web;

namespace Channelvn.Cached.Base
{
    public class ControlBase : UserControl
    {
        private bool m_ManualUpdate;
        public bool ManualUpdate
        {
            get { return this.m_ManualUpdate; }
            set { this.m_ManualUpdate = value; }
        }

        private bool is_DetailsData = false;
        public bool Is_DetailsData
        {
            get { return this.is_DetailsData; }
            set { this.is_DetailsData = value; }
        }

        private CacheSettings.ControlSetting m_ControlCacheSetting;
        public CacheSettings.ControlSetting ControlCacheSetting
        {
            get { return this.m_ControlCacheSetting; }
            set { this.m_ControlCacheSetting = value; }
        }

        private string m_ParentCategory;
        public string ParentCategory
        {
            get { return this.m_ParentCategory; }
            set { this.m_ParentCategory = value; }
        }

        public void SetProperty(string key, object value)
        {
            System.Reflection.PropertyInfo property = this.GetType().GetProperty(key);
            if (null != property)
            {
                property.SetValue(this, Convert.ChangeType(value, property.PropertyType), null);
            }
        }

        private string m_CacheContent;
        private string m_ControlCacheName = "";
        public string ControlCacheName
        {
            get
            {
                if (string.IsNullOrEmpty(m_ControlCacheName))
                {
                    m_ControlCacheName = "ControlID:" + ID;
                    if (ID.Contains("ExtraContentTop1")) return m_ControlCacheName;
                    if (ID.Contains("DynamicFocusBox")) return m_ControlCacheName + Lib.QueryString.CategoryID;
                    if (ID.Contains("ScrollTop")) return m_ControlCacheName;
                    m_ControlCacheName = m_ControlCacheName + "_NewsID:" + Lib.QueryString.NewsID;
                    return m_ControlCacheName;
                }
                else
                    return m_ControlCacheName;
            }
            set { this.m_ControlCacheName = value; }
        }

        public long CacheExpireTime = CacheController.DefauleExprieCategory * 365;

        public bool HasCache()
        {

            if (CacheController.IsAllowDistributedCached)
            {
                string updControl = HttpContext.Current.Request.QueryString["updControl"];
                if (!string.IsNullOrEmpty(updControl))
                {
                    if (updControl == "1") return false;
                }
                m_CacheContent = CacheController.Get<string>(Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE, ControlCacheName);
                if (!string.IsNullOrEmpty(m_CacheContent))
                {
                    //if (m_CacheContent.IndexOf("<div id='divDetail'></div>") != -1)
                    //{
                    //    m_CacheContent = "";
                    //    return false;
                    //}
                    return true;
                }
            }
            //m_CacheContent = "";
            return false;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(m_CacheContent))
            {
                StringBuilder strBuilder = new StringBuilder();
                using (StringWriter strWriter = new StringWriter(strBuilder))
                {
                    using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))
                    {
                        base.Render(htmlWriter);
                        m_CacheContent = strBuilder.ToString();
                        if (ConfigurationManager.AppSettings["AllowWrite1Line"] == "1")
                        {
                            m_CacheContent = m_CacheContent.Replace("\t", string.Empty);
                            m_CacheContent = m_CacheContent.Replace("    ", string.Empty);
                            m_CacheContent = m_CacheContent.Replace("  ", " ");
                        }

                        if (CacheController.IsAllowDistributedCached && is_DetailsData)
                        {
                            CacheController.Add(Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE, ControlCacheName, m_CacheContent, CacheExpireTime);
                        }
                    }
                }
            }
            writer.Write(m_CacheContent);
        }
    }
}
